package acciones;
import sun.misc.BASE64Encoder;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class encriptar {
	
	    public static  String encripta(String valorClave) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
		
		String claveEncriptada = null;
		
		String claveOriginal = valorClave;
		
		String semilla = "0123456789";
		
		 
		
		// Generamos una clave secreta.
		
		SecretKeySpec desKey = new SecretKeySpec(new String((semilla.trim().concat("99999999")).substring(0, 8)).getBytes(), "DES");
		
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		
		cipher.init(Cipher.ENCRYPT_MODE, desKey);
		
		byte[] claveEncriptadaBytes = cipher.doFinal( claveOriginal.getBytes() );
	
		claveEncriptada = new BASE64Encoder().encode( claveEncriptadaBytes );
		System.out.println("La clave es:"+claveEncriptada);
		return claveEncriptada;

	}
	    
	public static void main(String[] args) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
		String clave="0282484";
		String result="";
		result=encripta(clave);
		System.out.println("El resultado es:"+result);
	}    
		
}